package org.csu.molediary.api.controller.front;

import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Commentinfo;
import org.csu.molediary.api.entity.Diary;
import org.csu.molediary.api.entity.Good;
import org.csu.molediary.api.service.AccountService;
import org.csu.molediary.api.service.DiaryService;
import org.csu.molediary.api.service.GoodService;
import org.csu.molediary.api.vo.GoodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodController {
    @Autowired
    GoodService goodService;
    @Autowired
    AccountService accountService;
    @Autowired
    DiaryService diaryService;

    @PostMapping("/saveinformation")
    @ResponseBody
    //点赞加一
    public CommonResponse<Good> goodCommonResponse(
            @RequestParam ("goodone")String goodone,
            @RequestParam ("gooded")String gooded,
            @RequestParam ("diaryid")int diaryid
            ){
        Good good=new Good();
        good.setGoodone(goodone);
        good.setGooded(gooded);
        good.setDiaryid(diaryid);

        //被点赞人的点赞总数目加1
        Account account=accountService.selectAccountById(gooded).getData();
        account.setAccountgoodnum(account.getAccountgoodnum()+1);
        accountService.updateAccountInformation(account);

        //日记被点赞数量加1
        diaryService.addNumDiary(diaryid);

        return goodService.saveGood(good);

    }

    @GetMapping("information/{id}")
    @ResponseBody
    //通过被点赞人查找点赞信息
    public CommonResponse<List<GoodVo>> getInformationByGooded(@PathVariable("id")String gooded){
        return goodService.queryGoodByGooded(gooded);
    }

    @GetMapping("goodinformation/{id}")
    @ResponseBody
    //通过点赞人查找点赞信息
    public CommonResponse<List<Good>> getInformationByGoodOne(@PathVariable("id")String goodone){
        return goodService.queryGoodByGoodOne(goodone);
    }
}
